require('../reg/reg.less')
const { default: axios } = require('axios');
let tel = document.querySelector('.tel')
let mm = document.querySelector('.mm')
let btn = document.querySelector('button')
let zhu = document.querySelector('.zhu')

let flag = {
    telFlag: false,
    mmFlag: false
}

tel.addEventListener('blur', function () {
    if (tel.value.trim() == '') {
        flag.telFlag = false
        tel.style.border = '2px solid red'
    } else if (/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(tel.value.trim())) {
        flag.telFlag = true
        tel.style.border = '2px solid #0f0'
    } else {
        flag.telFlag = false
        tel.style.border = '2px solid red'
    }
})

mm.addEventListener('blur', function () {
    if (mm.value.trim() == '') {
        flag.mmFlag = false
        mm.style.border = '2px solid red'
    } else if (/^[0-9]*$/.test(mm.value.trim())) {
        flag.mmFlag = true
        mm.style.border = '2px solid #0f0'
    } else {
        flag.mmFlag = false
        mmm.style.border = '2px solid red'
    }
})

btn.addEventListener('click', function () {
    let arr = Object.values(flag)
    let zhenFlag = arr.every(function (item, index) {
        return item
    })
    if (zhenFlag) {
        axios.post('http://47.96.154.185:3701/api/user/login', {
            account: tel.value,
            password: mm.value
        })
            .then(function (res) {
                if(res.data.errno == 0){
                    localStorage.setItem('login',JSON.stringify(res.data.data))
                    location.href = './home.html'
                }else{
                    alert(res.data.message)
                }
                console.log(res);
            })
            .catch(function(err){})
    }
})

zhu.addEventListener('click', function () {
    location.href = '../login.html'
})